package com.example.springboot.service;

import com.example.springboot.dao.CovidData;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.core.io.Resource;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

@Service
public class DataService {

    @PostConstruct
    @Scheduled(cron = "0 0 1 * * *")
    public List<CovidData> GetData() throws IOException {
        List<CovidData> covidData = new ArrayList<>();

        String dataUri1 = "https://gitee.com/dgut-sai/COVID-19/raw/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        String dataUri = "https://github.com/CSSEGISandData/COVID-19/blob/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        String dataUri2 = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
        //爬取全球新型冠状病毒统计数据的csv文件
        RequestEntity<Void> requestEntity = RequestEntity
                .get(URI.create(dataUri1))
                .headers(httpHeaders -> httpHeaders.add( "User-Agent","GJF"))
                .build();
        ResponseEntity<Resource> responseEntity = new RestTemplate().exchange(requestEntity,Resource.class);
        Resource resource = responseEntity.getBody();

        InputStream inputStream = resource.getInputStream();


        SaveOriginData(resource);//原始数据

        InputStreamReader reader = new InputStreamReader(inputStream);

        Reader in = new BufferedReader(reader);
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in);
        for (CSVRecord record : records) {
            CovidData data = new CovidData();
            data.setState(record.get("Province/State"));
            data.setCountry(record.get("Country/Region"));
            data.setCurrentNum(Integer.parseInt((record.get(record.size()-1))));
            data.setNewCases(Integer.parseInt(record.get(record.size()-1))-Integer.parseInt(record.get(record.size()-2)));
            covidData.add(data);
        }

        return covidData;
    }

    public void SaveOriginData(Resource resource) throws IOException {
        InputStream inputStream = resource.getInputStream();

        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        in = new BufferedInputStream(inputStream);
        out = new BufferedOutputStream(new FileOutputStream("D:\\IDEA\\IdeaProjects\\Lab3\\src\\main\\resources\\static\\data.csv") );

        int len = -1;
        byte[] b = new byte[1024];
        while((len = in.read(b))!=-1){
            out.write(b,0,len);
        }
        in.close();
        out.close();
    }

    public List<CovidData> ReadDataFromFile() throws IOException {
        List<CovidData> covidData = new ArrayList<>();

        String path = "D:\\IDEA\\IdeaProjects\\Lab3\\src\\main\\resources\\static\\data.csv";
        BufferedReader reader = new BufferedReader(new FileReader(path));

        Reader in = new BufferedReader(reader);
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(in);
        for (CSVRecord record : records) {
            CovidData data = new CovidData();
            data.setState(record.get("Province/State"));
            data.setCountry(record.get("Country/Region"));
            data.setCurrentNum(Integer.parseInt((record.get(record.size()-1))));
            data.setNewCases(Integer.parseInt(record.get(record.size()-1))-Integer.parseInt(record.get(record.size()-2)));
            covidData.add(data);
        }

        return covidData;
    }


}
